﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public static class TableController 
    {
        private const string ITEM_CTRL_PREID = "KPItem_";

        public static Table CreateTable(VW_ZB_ItemResult vwResult, bool IsShow)
        {

            KP_Subject_Employee se = new KP_Subject_Employee();
            ZB_Tender tender = new ZB_Tender();
            string TenderName = "";

            se.Load(vwResult.SubID);
            int nTenderCount = se.RowCount;
            int TableWidth = 720 + nTenderCount * 60;
            //创建表头
            Table tb = new Table();
            tb.ID = "dtable";
            TableRow tr = new TableRow();
            tr.TableSection = TableRowSection.TableHeader; //For header
            TableCell tc = new TableCell();
            //tb.Attributes.Add("style", zTable.TableStyle);
            tb.Attributes.Add("border", "1");
            //tb.Attributes.Add("class", "ms-formlabel");
            tb.Attributes.Add("width", TableWidth + "px");
            tb.Attributes.Add("cellpadding", "0");
            tb.Attributes.Add("cellspacing", "0");

            KP_Subject_Table_Ump su = new KP_Subject_Table_Ump();
            su.Where.SubjectID.Value = vwResult.SubID;
            //若是参考项目，则现示所有评分表
            if (!IsShow) su.Where.UmpID.Value = vwResult.UmpID;
            su.Where.UmpType.Value = 0;//评分
            su.Where.KpTableID.Value = vwResult.KpID;
            su.Query.Load();
            int nUmpCount = su.RowCount;

            KP_Master kpTable = new KP_Master();
            kpTable.LoadByPrimaryKey(vwResult.KpID);

            tr = new TableRow();
            tr.TableSection = TableRowSection.TableHeader; //For header
            tc = HelperTable.CreateStyleCell(1, 6 + nTenderCount, kpTable.Title, "font-size: 18.0pt;font-weight: 700;font-style: normal;text-decoration: none;font-family: 黑体;text-align: center;vertical-align: middle;white-space: normal;");
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);

            //加入项目信息
            tr = new TableRow();
            tr.TableSection = TableRowSection.TableHeader; //For header
            KP_Subject subject = new KP_Subject();
            subject.LoadByPrimaryKey(vwResult.SubID);
            int nCol1 = (6 + nTenderCount) / 2;
            int nCol2 = (6 + nTenderCount) - nCol1;
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol1, "项目名称：" + subject.SubjectName, HelperTable.ALIGN_LEFT));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol2, "项目编号：" + subject.Code, HelperTable.ALIGN_RIGHT));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.TableSection = TableRowSection.TableHeader; //For header
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol1, "评标时间：" + DateTime.Now.ToString(), HelperTable.ALIGN_LEFT));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, nCol2, "评标地点：" + subject.Address, HelperTable.ALIGN_RIGHT));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.TableSection = TableRowSection.TableHeader; //For header
            tc = HelperTable.CreateCell(2, 1, "序号");
            tc.Style.Add("width", "20px");
            tr.Cells.Add(tc);

            tc = HelperTable.CreateCell(2, 1, "评议指标");
            tc.Style.Add("width", "50px");
            tr.Cells.Add(tc);

            tc = HelperTable.CreateCell(2, 1, "指标内容");
            tc.Style.Add("width", "150px");
            tr.Cells.Add(tc);

            tc = HelperTable.CreateCell(2, 1, "具体要求");
            tc.Style.Add("width", "200px");
            tr.Cells.Add(tc);

            tc = HelperTable.CreateCell(2, 1, "分值");
            tc.Style.Add("width", "50px");
            tr.Cells.Add(tc);

            tr.Cells.Add(HelperTable.CreateCell(1, nTenderCount, "投标人"));

            tc = HelperTable.CreateCell(2, 1, "评分细则");
            tc.Style.Add("width", "250px");
            tr.Cells.Add(tc);

            tb.Rows.Add(tr);

            //表头投标公司
            tr = new TableRow();
            tr.TableSection = TableRowSection.TableHeader; //For header
            se.Rewind();
            while (!se.EOF)
            {
                tender.LoadByPrimaryKey(se.EmployeeID);
                TenderName = tender.Name;
                tr.Cells.Add(HelperTable.CreateCell(1, 1, TenderName));

                se.MoveNext();
            }
            tb.Rows.Add(tr);


            KP_Template template = new KP_Template();
            template.Where.KP_ID.Value = vwResult.KpID;
            template.Where.Is_Group.Value = 0;
            template.Query.Load();
            int nItemCount = template.RowCount;

            template = new KP_Template();
            template.Where.KP_ID.Value = vwResult.KpID;
            template.Where.Is_Group.Value = 1;
            template.Query.Load();
            int nGroupCount = template.RowCount;

            int RowNo = 1;
            while (!su.EOF)
            {
                tr = new TableRow();
                tr.TableSection = TableRowSection.TableBody;
                DataRow[] drs = vwResult.DefaultView.Table.Select("UmpID=" + su.UmpID);
                string UmpName = drs[0][VW_ZB_ItemResult.ColumnNames.UmpName].ToString();

                tr.Cells.Add(HelperTable.CreateCell(nItemCount, 1, (RowNo++).ToString()));
                //评审表类型
                KP_Master master = new KP_Master();
                master.LoadByPrimaryKey(vwResult.KpID);
                KP_Category cat = new KP_Category();
                cat.LoadByPrimaryKey(master.KP_Category_ID);
                tr.Cells.Add(HelperTable.CreateCell(nItemCount, 1, cat.KP_Category_Name));

                //添加指标集
                while (!template.EOF)
                {
                    KP_Template itemTemplate = new KP_Template();
                    itemTemplate.Where.Parent_Item_ID.Value = template.Item_ID;
                    itemTemplate.Query.Load();
                    tc = HelperTable.CreateCell(itemTemplate.RowCount, 1, "");
                    //tc.Controls.Add(new LiteralControl(template.Item_Name + "(权重" + (template.Item_Quotiety * 100).ToString() + "%)"));
                    tc.Controls.Add(new LiteralControl(template.Item_Name));// + "(权重" + (template.Item_Quotiety * 100).ToString() + "%)"));
                    if (!IsShow)
                    {
                        se.Rewind();
                        while (!se.EOF)
                        {
                            tender.LoadByPrimaryKey(se.EmployeeID);
                            TenderName = tender.Name;
                            CreateScoreItem(vwResult, su.UmpID, template.Item_ID, se.EmployeeID, tc);

                            se.MoveNext();
                        }
                    }

                    //tc.Text = ;

                    tr.Cells.Add(tc);

                    //添加指标项目
                    while (!itemTemplate.EOF)
                    {
                        //                        tr.Cells.Add(HelperTable.CreateCell(1, 1, itemTemplate.Item_Name + "(权重" + (itemTemplate.Item_Quotiety * 100).ToString() + "%)"));
                        tr.Cells.Add(HelperTable.CreateCell(1, 1, itemTemplate.Item_Name));// + "(权重" + (itemTemplate.Item_Quotiety * 100).ToString() + "%)"));
                        tr.Cells.Add(HelperTable.CreateCell(1, 1, itemTemplate.Max_Score.ToString("N2")));

                        se.Rewind();
                        while (!se.EOF)
                        {
                            DataRow[] items = vwResult.DefaultView.Table.Select("UmpID=" + su.UmpID + " and ItemID= " + itemTemplate.Item_ID + " and TenderID=" + se.EmployeeID);
                            string sScore = items[0][VW_ZB_ItemResult.ColumnNames.ItemScore].ToString();
                            string sText = items[0][VW_ZB_ItemResult.ColumnNames.ItemText].ToString();
                            tc = HelperTable.CreateStyleCell(1, 1, "", HelperTable.ALIGN_CENTER);
                            tc.Style.Add("width", "50px");

                            if (!IsShow)
                            {
                                CreateScoreItem(vwResult, su.UmpID, itemTemplate.Item_ID, se.EmployeeID, tc);
                            }
                            else
                            {
                                tc.Text = GetItemScore(itemTemplate.Item_Type.ToString(), sScore, sText);
                            }
                            tr.Cells.Add(tc);

                            se.MoveNext();
                        }

                        // 指标项评分描述
                        //tc = new TableCell();
                        //tc.Text = itemTemplate.Item_Desc;
                        tc = HelperTable.CreateStyleCell(1, 1, itemTemplate.Item_Desc, HelperTable.ALIGN_LEFT);

                        tr.Cells.Add(tc);

                        tb.Rows.Add(tr);
                        tr = new TableRow();

                        itemTemplate.MoveNext();
                    }

                    template.MoveNext();
                }

                //表脚
                tr = new TableRow();
                tr.TableSection = TableRowSection.TableFooter; //For TableFooter
                tr.Cells.Add(HelperTable.CreateCell(1, 4, "总分"));
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, kpTable.KpScore.ToString("N2"), HelperTable.ALIGN_CENTER));

                se.Rewind();
                while (!se.EOF)
                {
                    DataRow[] items = vwResult.DefaultView.Table.Select("UmpID=" + su.UmpID + " and TenderID=" + se.EmployeeID);
                    string sScore = items[0][VW_ZB_ItemResult.ColumnNames.EntryScore].ToString();
                    tc = HelperTable.CreateStyleCell(1, 1, sScore, HelperTable.ALIGN_CENTER);
                    if (!IsShow)
                    {
                        CreateTotalItem(vwResult, su.UmpID, se.EmployeeID, tc);
                    }
                    else
                    {
                        tc.Text = sScore;
                    }

                    tr.Cells.Add(tc);
                    se.MoveNext();
                }
                tr.Cells.Add(HelperTable.CreateCell(1, 1, "按权重汇总分数"));
                tb.Rows.Add(tr);

                tr = new TableRow();
                tr.TableSection = TableRowSection.TableFooter; //For TableFooter
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 5, "评委：____________________________" + vwResult.UmpName, HelperTable.ALIGN_LEFT));
                tr.Cells.Add(HelperTable.CreateCell(1, 1 + nTenderCount));
                tb.Rows.Add(tr);

                tr = new TableRow();
                tr.TableSection = TableRowSection.TableFooter; //For TableFooter
                tr.Cells.Add(HelperTable.CreateCell(1, 5));
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + nTenderCount, "湖北信通通信有限公司惠达招标分公司", HelperTable.ALIGN_RIGHT));
                tb.Rows.Add(tr);

                //一个评分表创建完成;

                su.MoveNext();
            }


            return tb;

        }

        public static void CreateScoreItem(VW_ZB_ItemResult vwResult, int UmpID, int ItemID, int TenderID, TableCell tc)
        {
            TextBox txtItem = new TextBox();
            DataRow[] items = vwResult.DefaultView.Table.Select("UmpID=" + UmpID + " and TenderID=" + TenderID + " and itemID=" + ItemID);
            string sScore = items[0][VW_ZB_ItemResult.ColumnNames.ItemScore].ToString();
            string sText = items[0][VW_ZB_ItemResult.ColumnNames.ItemText].ToString();
            string sItemName = items[0][VW_ZB_ItemResult.ColumnNames.ItemName].ToString();
            string MaxScore = items[0][VW_ZB_ItemResult.ColumnNames.MaxScore].ToString();
            string MinScore = items[0][VW_ZB_ItemResult.ColumnNames.MinScore].ToString();
            string TenderName = items[0][VW_ZB_ItemResult.ColumnNames.TenderName].ToString();
            int ItemWidth = HelperDigit.ConvertToInt32(items[0][VW_ZB_ItemResult.ColumnNames.Item_Width].ToString());
            string sType = items[0][VW_ZB_ItemResult.ColumnNames.Item_Type].ToString();
            bool bIsGroup = bool.Parse(items[0][VW_ZB_ItemResult.ColumnNames.Is_Group].ToString());

            txtItem.ID = ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
            txtItem.ToolTip = TenderID.ToString();
            txtItem.Width = Unit.Pixel(ItemWidth);
            txtItem.Text = GetItemScore(sType, sScore, sText);

            txtItem.Style.Add("text-align", "center"); //text-align: center
            txtItem.Style.Add("background-color", "transparent"); 

            if (sType.Equals("1"))
            {
                txtItem.TextMode = TextBoxMode.MultiLine;
                txtItem.Height = Unit.Pixel(54);
            }
            else
            {
                string sMSG = String.Format("<font color=red>评分项：{0}</font><br><font color=bule>竞标人：{1}</font><br><font color=fuchsia>应答情况：{2}</font>", sItemName, TenderName, sText);
                string sMessage = String.Format("this.select();popHint(this, '{0}');", sMSG);
                if (!bIsGroup) txtItem.Attributes.Add("onfocus", sMessage);
            }

            tc.Controls.Add(txtItem);

            // 添加评分有效性验证控件
            RequiredFieldValidator validator = new RequiredFieldValidator();
            validator.ID = "RFV_" + ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
            validator.ControlToValidate = ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
            //validator.ClientValidationFunction = "is_valid_score";
            validator.ErrorMessage = "请评分项－" + TenderName + "-" + sItemName;
            validator.Text = "*";
            validator.Display = ValidatorDisplay.Dynamic;
            validator.SetFocusOnError = true;

            tc.Controls.Add(validator);

            //添加数值型或字符型验证
            //0为数值型验证
            if (sType.Equals("0"))
            {
                //RegularExpressionValidator rev = new RegularExpressionValidator();
                //rev.ID = "REV_" + ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
                //rev.ControlToValidate = ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
                ////rev.ClientValidationFunction = "is_valid_score";
                //rev.ValidationExpression = @"^\d+(\.\d+)?$";
                //rev.ErrorMessage = "请填写一个数值－" + vwResult.TenderName + "-" + sItemName;
                //rev.SetFocusOnError = true;
                //rev.Text = "*";
                //rev.Display = ValidatorDisplay.Dynamic;
                //tc.Controls.Add(rev);

                RangeValidator rev = new RangeValidator();
                rev.ID = "REV_" + ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
                rev.ControlToValidate = ITEM_CTRL_PREID + ItemID.ToString() + "_" + TenderID.ToString();
                rev.Type = ValidationDataType.Double;
                rev.MaximumValue = MaxScore;
                rev.MinimumValue = MinScore;
                string sMsg = String.Format("请填写一个正确的数值({0}-{1})－竞标人：{2}-指标项：{3}", rev.MinimumValue, rev.MaximumValue, TenderName, sItemName);
                rev.ErrorMessage = sMsg;
                rev.SetFocusOnError = true;
                rev.Text = "*";
                rev.Display = ValidatorDisplay.Dynamic;
                tc.Controls.Add(rev);
            }
        }

        //选择是数字还是文本
        public static string GetItemScore(string sType, string sScore, string sText)
        {
            string sStr = "";
            if (sType.Equals("0"))
            {
                sStr = sScore;
            }
            else
            {
                sStr = sText;
            }

            return sStr;
        }

        public static void CreateTotalItem(VW_ZB_ItemResult vwResult, int UmpID, int TenderID, TableCell tc)
        {
            Label lbItemScore = new Label();
            DataRow[] items = vwResult.DefaultView.Table.Select("UmpID=" + UmpID + " and TenderID=" + TenderID);
            string sScore = items[0][VW_ZB_ItemResult.ColumnNames.EntryScore].ToString();

            lbItemScore.ID = "lb_" + ITEM_CTRL_PREID + TenderID.ToString();
            lbItemScore.Text = sScore;
            tc.Controls.Add(lbItemScore);

        }

    }
}
